﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class ParametersDocument : Document
    {
        #region internal
        //Constructor
        internal ParametersDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Parameters;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "parameters.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Parameters"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Parameters</h2>");

                #region Parameters Body
                var sprocParameters = (from schema in _database.Schemas
                                       from storedProcedure in schema.StoredProcedures
                                       from parameter in storedProcedure.Parameters
                                       select parameter);
                var funcParameters = (from schema in _database.Schemas
                                      from function in schema.Functions
                                      from parameter in function.Parameters
                                      select parameter);
                var parameters = (from parameter in sprocParameters.Union(funcParameters)
                                  orderby parameter.Name
                                  select parameter);
                var parametersArray = parameters.ToArray<SqlServer.Parameter>();

                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""usedByColumn"" /></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>Used By</th></tr>");

                Boolean evenRow = true;
                StringBuilder sb = new StringBuilder();
                for (int rowIndex = 0; rowIndex < parametersArray.Length; rowIndex++)
                {
                    //If this is the column in the list or the next column is different we need to start a new row.
                    if (rowIndex == 0 || parametersArray[rowIndex].Name != parametersArray[rowIndex - 1].Name)
                    {
                        if (evenRow == true)
                            sb.Append(@"<tr class=""evenRow"">");
                        else
                            sb.Append(@"<tr class=""oddRow"">");
                        evenRow = !evenRow;

                        if (parametersArray[rowIndex].Name == "")
                            sb.Append(@"<td>Return Value</td><td>");
                        else
                            sb.Append(@"<td>" + ScrubHtml(parametersArray[rowIndex].Name) + @"</td><td>");
                    }
                    //either way we need to add this column to the list.
                    if (parametersArray[rowIndex].StoredProcedure != null)
                    {
                       if (Business.FilterPreferences.ShowStoredProceduresPage)
                       {
                          string href = @"<a href=""http://index." +
                          RootDatabase.Name + @"." + parametersArray[rowIndex].StoredProcedure.Schema.Name
                          + @".storedprocedure." +
                          parametersArray[rowIndex].StoredProcedure.Name + @".html"">" +
                          ScrubHtml(parametersArray[rowIndex].StoredProcedure.Schema.Name) + "." +
                          ScrubHtml(parametersArray[rowIndex].StoredProcedure.Name) + @"</a>, ";
                          sb.Append(href);
                       }
                       else
                       {
                          string href = ScrubHtml(parametersArray[rowIndex].StoredProcedure.Schema.Name) + "." +
                          ScrubHtml(parametersArray[rowIndex].StoredProcedure.Name) + @", ";
                          sb.Append(href);
                       }
                    }
                    else
                    {
                       if (Business.FilterPreferences.ShowFunctionsPage)
                       {
                          string href = @"<a href=""http://index." +
                          RootDatabase.Name + @"." + parametersArray[rowIndex].Function.Schema.Name
                          + @".function." +
                          parametersArray[rowIndex].Function.Name + @".html"">" +
                          ScrubHtml(parametersArray[rowIndex].Function.Schema.Name) + "." +
                          ScrubHtml(parametersArray[rowIndex].Function.Name) + @"</a>, ";
                          sb.Append(href);
                       }
                       else
                       {
                          string href = ScrubHtml(parametersArray[rowIndex].Function.Schema.Name) + "." +
                          ScrubHtml(parametersArray[rowIndex].Function.Name) + @", ";
                          sb.Append(href);
                       }
                    }

                    //Finally if this is the last column or the next column is different then we need to end this row
                    if (rowIndex == parametersArray.Length - 1 || parametersArray[rowIndex].Name != parametersArray[rowIndex + 1].Name)
                    {
                        string column = sb.ToString();
                        column = column.TrimEnd(", ".ToCharArray());
                        AddBodyLine(column + @"</td></tr>");
                        sb = new StringBuilder();
                    }
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");

                _html = CreateHtmlDocument();
                #endregion
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
